﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunHan.BuShi123.BLL;
using YunHan.BuShi123.Model;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }

    public int PageIndex
    {
        get
        {
            return Request["page"] == null ? 1 : Convert.ToInt32(Request["page"]);
        }
    }
    public int PageSize { get { return 30; } }

    public string keyword
    {
        get
        {
            if (Request.Form["keyword"] != null)
            {
                return Request.Form["keyword"].ToString();
            }
            else if (Request.QueryString["keyword"] != null)
            {
                return Request.QueryString["keyword"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private void Bind()
    {
        string Where = "";
        List<SqlParameter> Parms = new List<SqlParameter>();
        if (keyword.Length > 0)
        {
            Where += "Title LIKE @Title";
            Parms.Add(new SqlParameter("@Title", "%" + keyword + "%"));

            HotWord_Model Hot = HotWord_BLL.GetModel("Word=@Word", new SqlParameter("@Word", keyword));
            if (Hot != null)
            {
                Hot.Num++;
                HotWord_BLL.Update("Num=@Num", "ID=@ID", Hot);
            }
            else
            {
                Hot = new HotWord_Model();
                Hot.Num = 1;
                Hot.Word = keyword;
                HotWord_BLL.Add(Hot);
            }
        }

        int R = 0;
        repRecord.DataSource = Record_BLL.GetModelListByPage(ref R, Where, "", "ID DESC", PageSize, PageIndex, Parms.ToArray());
        repRecord.DataBind();

        #region 分页条
        int TotalPage = R / PageSize + 1;
        string PageHtml = "<tr><td colspan='4' class='page_link'>";
        if (PageIndex > 1)
        {
            PageHtml += string.Format("<a href='?keyword={1}&page=1'>首页</a>&nbsp;&nbsp;<a href='?keyword={1}&page={0}'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;", PageIndex - 1, Request["keyword"]);
        }
        if (PageIndex < TotalPage)
        {
            PageHtml += string.Format("<a href='?keyword={2}&page={0}'>下一页</a><a href='?keyword={2}&page={1}'>末页&nbsp;&nbsp;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", PageIndex + 1, TotalPage, Request["keyword"]);
        }
        PageHtml += string.Format("一共<span class='current'>{0}条记录&nbsp;&nbsp;&nbsp;每页<span class='current'>{3}</span>条&nbsp;&nbsp;&nbsp;分<span class='current'>{1}</span>页&nbsp;&nbsp;当前第<span class='current'>{2}</span>页<div class='clear_both'></div>", R, TotalPage, PageIndex, PageSize);

        for (int i = PageIndex; i <= PageIndex + 10; i++)
        {
            if (i <= TotalPage)
            {
                PageHtml += string.Format("<a href='?keyword={1}&page={0}' class='current'>[{0}]</a>", i, Request["keyword"]);
            }
        }
        PageHtml += "</td></tr>";
        litBottomPager.Text = PageHtml;
        litTopPager.Text = PageHtml;
        #endregion

        litSearchCount.Text = HotWord_BLL.GetListCount("").ToString();
        litSearchHot.Text = "";
        List<HotWord_Model> HotList = HotWord_BLL.GetModelList("TOP 10 *", "", "Num DESC");
        foreach (var item in HotList)
        {
            litSearchHot.Text += string.Format("<a href=\"?keyword={0}\">{1}/{2}次</a>&nbsp;&nbsp;&nbsp;&nbsp;", Server.UrlEncode(item.Word), item.Word, item.Num);
        }

        #region 自定义编辑区域
        litNav.Text = Info_BLL.GetModel("title='顶部导航条'").context;
        litAd.Text = Info_BLL.GetModel("title='广告栏'").context;
        litAdLeft.Text = Info_BLL.GetModel("title='左侧广告栏'").context;
        litAdRight.Text = Info_BLL.GetModel("title='右侧广告栏'").context;
        #endregion

        #region 友情链接
        //string AdHtml = "<tr>";
        //List<Friendship_Model> FriendList = Friendship_BLL.GetModelList("", "", "Sort DESC");
        //for (int i = 0; i < FriendList.Count; i++)
        //{

        //    AdHtml += string.Format("<td><a href=\"{0}\" rel=\"nofollow\" target=\"_blank\"><img src=\"{1}\" width=\"243\" height=\"60\" border=\"0\" align=\"middle\" /></a></td>", FriendList[i].url ,FriendList[i].Logo);
        //    if (i > 0 && i % 4 == 0)
        //    {
        //        AdHtml += "</tr><tr>";
        //    }
        //}
        //AdHtml = AdHtml.Remove(AdHtml.Length - 5, 5);
        //litAdHtml.Text = AdHtml;
        #endregion
    }
}